package PAT;

import java.util.LinkedList;
import java.util.Scanner;

class TreeNode{
    public int val;
    public TreeNode left,right;
}

public class PAT1020 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] post = new int[n];
        int[] in = new int[n];
        for (int i = 0; i < n; i++) {
            post[i] = scanner.nextInt();
        }
        for (int i = 0; i < n; i++) {
            in[i] = scanner.nextInt();
        }
        TreeNode root = getTree(post,in,0,n-1,0,n-1);
        bfs(root,n);

    }

    private static void bfs(TreeNode root,int n) {
        if(root==null )return;
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.addLast(root);
        int t = 1;
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            System.out.print(pop.val);
            if(pop.left!=null) stack.addLast(pop.left);
            if(pop.right!=null) stack.addLast(pop.right);
            if(t++<n){
                System.out.print(" ");
            }
        }
    }

    private static TreeNode getTree(int[] post, int[] in, int postl, int postr, int inl, int inr) {
        if(postl>postr || inl>inr) return null;
        int o = post[postr];
        int index =inl ;
        for (int i = inl; i < inr; i++) {
            if(in[i]==o){
                index = i;
                break;
            }
        }
        int len = index - inl;
        TreeNode treeNode = new TreeNode();
        treeNode.val = o;
        treeNode.left = getTree(post,in,postl,postl+len-1,inl,index-1);
        treeNode.right = getTree(post,in,postl+len,postr-1,index+1,inr);
        return treeNode;
    }

}
